<template>
  <el-form ref="formRef" :model="form" label-width="120px" status-icon>
    <el-form-item label="上传类型" prop="type">
      <el-radio-group v-model="form.mode">
        <el-radio label="local">本地上传</el-radio>
        <el-radio label="oss">阿里云OSS</el-radio>
      </el-radio-group>
    </el-form-item>
    <div>
      <el-form-item label="文件域名" prop="upload_path">
        <el-input v-model="form.domain" placeholder="请输入文件域名" clearable />
      </el-form-item>
    </div>
    <div v-if="form.mode === 'local'">
      <el-form-item label="上传目录" prop="upload_path">
        <el-input v-model="form.upload_path" placeholder="请输入字典名称" clearable />
      </el-form-item>
    </div>

    <div v-if="form.mode === 'oss'">
      <el-form-item label="accessKeyId" prop="accessKeyId">
        <el-input v-model="form.accessKeyId" placeholder="请输入accessKeyId" clearable />
      </el-form-item>
      <el-form-item label="accessKeySecret" prop="accessKeySecret">
        <el-input v-model="form.accessKeySecret" placeholder="请输入accessKeySecret" clearable />
      </el-form-item>
      <el-form-item label="bucket" prop="bucket">
        <el-input v-model="form.bucket" placeholder="请输入bucket" clearable />
      </el-form-item>
      <el-form-item label="endpoint" prop="endpoint">
        <el-input v-model="form.endpoint" placeholder="请输入endpoint" clearable />
      </el-form-item>
    </div>

    <el-form-item>
      <el-button type="primary" plain v-debounce="handleSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getConfig } from "@/api/system/config";

// 定义参数的类型
interface IConfigProps {
  configKey?: string;
  name?: string;
}

// 子组件接收父组件的值
// withDefaults：设置默认值  defineProps：接收父组件的参数
const props = withDefaults(defineProps<IConfigProps>(), {
  name: "",
  configKey: ""
});

onMounted(() => {
  getConfig({ key: props.configKey }).then(res => {
    if (Object.keys(res.data).length>0){
      form.value = res.data;
    }
  });
});

// 当前组件获取父组件传递的事件方法
const emits = defineEmits(["handleSubmit"]);
// 弹框的确定事件
const handleSubmit = () => {
  emits("handleSubmit", form.value);
};
let form = ref<any>({
  mode: "local",
  domain: "",
  upload_path: "",
  accessKeyId: "",
  accessKeySecret: "",
  bucket: ""
});
</script>

<style lang="scss" scoped></style>
